<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {_pager} function plugin
 *
 * Type:     function<br>
 * Name:     _pager<br>
 * Date:     Dec 12, 2008<br>
 * Purpose:  make a pager for elements-list<br>
 * Input:<br>
 * map		$list		items to show. usually contains all page numbers in list
 * integer	$current	current item, for selecting
 * integer	$ends = 1	links quantity at ends
 * integer	$parallax = 4	left|right parallax for concrete item
 * string	$url = "/@p/"	template for url-generating
 *
 * Examples:
 * @author Alex Yaroshevich <qfox@ya.ru>
 * @version 0.1b
 * @link http://docs.commercial.su/view:smarty {_pager}
 * @param array 	$params
 * @param Smarty	&$smarty
 * @return string
 */
function smarty_function__pager( $params, &$smarty )
{
	if( isset( $params['data'] ) && is_array( $params['data'] ) )
	{
		$data = $params['data'];
		unset( $params['data'] );
		$data += $params;
		$params = $data;
	}
	
	if( !is_array( $params['list'] )
	 || !is_scalar( $params['current'] ) ) return "";
	
	$defaults = array(
		'ends'		=> 1,
		'parallax'	=> 4,
		'showall'	=> false
		);
	foreach( array_keys( $defaults ) as $k )
		$params[ $k ] = isset( $params[ $k ] ) ? $params[ $k ] : $defaults[ $k ];
	
	if( !isset($params['url_template']) )
	{
		$url = preg_replace( '@/page\d+/@i', '/page@p/', Request::__getMe( ) ->getUrl( ) );
		if( strpos( $url, 'page@p' ) === false ) $url .= 'page@p/';
		$params['url_template'] = $url;
	}

	$params['url_template'] = (string)($params['url_template']);
	$params['url_template'] = str_replace( '%40p', '@p', $params['url_template'] );

	$template = !isset($data['template']) ? '_pager.sma' : $data['template'];
	
	return view::__getMe() ->display( 'bits/'.$template, $params );
}

?>